import {app, BrowserWindow, Menu, ipcMain} from 'electron'

const fs = require('fs');
//import '../renderer/store'

/**
 * Set `__static` path to static files in production
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-static-assets.html
 */
if (process.env.NODE_ENV !== 'development') {
    global.__static = require('path').join(__dirname, '/static').replace(/\\/g, '\\\\')
}

let mainWindow
const winURL = process.env.NODE_ENV === 'development' ? `http://localhost:9080` : `file://${__dirname}/index.html`

function createWindow() {
    if (process.env.NODE_ENV !== 'development') {
        const menu = Menu.buildFromTemplate([])
        Menu.setApplicationMenu(menu);
    }

    /**
     * Initial window options
     */
    mainWindow = new BrowserWindow({
        useContentSize: true, icon: "./icon.ico", show: false, // minWidth: 1300,
        maxWidth: 1080,
        maxHeight: 1920,
        width: 1080 / 2,
        height: 1920 / 2,
        minWidth: 1080 / 2,
        minHeight: 1920 / 2,
        webPreferences: {
            nodeIntegration: true, contextIsolation: false, webSecurity: false
        }
    })


    // mainWindow.maximize()
    mainWindow.show()

    mainWindow.loadURL(winURL)

    mainWindow.on('closed', () => {
        mainWindow = null
    })
}

app.on('ready', createWindow)

app.on('window-all-closed', () => {
    if (process.platform !== 'darwin') {
        app.quit()
    }
})

app.on('activate', () => {
    if (mainWindow === null) {
        createWindow()
    }
})

ipcMain.on('interface-name', (event, arg) => {
    // 处理接收到的数据
    console.log(fs)
    console.log('文件路径', arg); // 打印发送的数据
    fs.readdir(arg, (err, files) => {
        if (err) {
            console.error('Error reading directory:', err);
            return;
        }
        event.reply('interface-response', JSON.stringify(files)); // 响应数据
    });
});
/**
 * Auto Updater
 *
 * Uncomment the following code below and install `electron-updater` to
 * support auto updating. Code Signing with a valid certificate is required.
 * https://simulatedgreg.gitbooks.io/electron-vue/content/en/using-electron-builder.html#auto-updating
 */

/*
import { autoUpdater } from 'electron-updater'

autoUpdater.on('update-downloaded', () => {
  autoUpdater.quitAndInstall()
})

app.on('ready', () => {
  if (process.env.NODE_ENV === 'production') autoUpdater.checkForUpdates()
})
 */